package com.phantasy.scene.sceneUnit.map
{
	import com.phantasy.common.GlobalObjectManager;
	import com.phantasy.loaders.gameLoader.GameLoader;
	import com.phantasy.loaders.gameLoader.ResourceItem;
	import com.phantasy.scene.sceneManager.LoopManager;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.utils.Dictionary;
	
	import mx.utils.NameUtil;
	
	public class MapBackGround extends Sprite
	{
		public static const MAP_TILE_WIDTH:int = 300;
		public static const MAP_TILE_HEIGHT:int = 300;
		public static const MAP_PATH:String = "assets/maps/";
		
		private var mapHash:Dictionary;
		private var loadingHash:Dictionary;
		private var mapURL:String;
		private var maxRow:int;
		private var maxColumn:int;
		private var bigMapWidth:Number;
		private var bigMapHeight:Number;
		private var startX:int;
		private var startY:int;
		private var endX:int;
		private var endY:int;
		private var midX:int;
		private var midY:int;
		private var sortIndex:int;
		private var imgURL:String;
		private var tileID:String;
		private var waitLoadList:Array;
		private var loading:Boolean = false;
		public function MapBackGround()
		{
			super();
			mouseChildren = mouseEnabled = false;
		}
		
		/**
		 *开始初始化地图背景 
		 * @param mapID
		 * @param mapWidth
		 * @param mapHeight
		 * 
		 */		
		public function startInitMap(mapID:int,mapWidth:Number,mapHeight:Number):void
		{
			this.bigMapWidth = mapWidth;
			this.bigMapHeight = mapHeight;
			this.loadingHash = new Dictionary(true);
			this.mapHash = new Dictionary();
			this.maxColumn = Math.ceil(mapWidth/MAP_TILE_WIDTH);
			this.maxRow = Math.ceil(mapHeight/MAP_TILE_HEIGHT);
			this.loading=false;
		}
		
		public function createBlur(_arg1:BitmapData):void
		{
			var _local2:Matrix = new Matrix();
			_local2.a = (this.bigMapWidth / _arg1.width);
			_local2.d = (this.bigMapHeight / _arg1.height);
			graphics.clear();
			graphics.beginBitmapFill(_arg1, _local2, false, false);
			graphics.drawRect(0, 0, this.bigMapWidth, this.bigMapHeight);
			graphics.endFill();
		}
		
		public function loadMap(_arg1:Number, _arg2:Number):void
		{
			var _local11:int;
			var _local12:int;
			var _local13:int;
			var _local3:int = (-(_arg1) / MAP_TILE_WIDTH);
			var _local4:int = (-(_arg2) / MAP_TILE_HEIGHT);
			var _local5:int = ((-(_arg1) + GlobalObjectManager.GAME_WIDTH) / MAP_TILE_WIDTH);
			var _local6:int = ((-(_arg2) + GlobalObjectManager.GAME_HEIGHT) / MAP_TILE_HEIGHT);
			if ((((((((_local3 == this.startX)) && ((_local4 == this.startY)))) && ((_local5 == this.endX)))) && ((_local6 == this.endY))))
			{
				return;
			};
			this.startX = _local3;
			this.startY = _local4;
			this.endX = _local5;
			this.endY = _local6;
			var _local7 = ((this.startX + this.endX) >> 1);
			var _local8 = ((this.startY + this.endY) >> 1);
			var _local9:Array = [];
			var _local10:int = this.startY;
			while (_local10 <= this.endY)
			{
				_local11 = this.startX;
				while (_local11 <= this.endX)
				{
					this.tileID = ((_local10 + "_") + _local11);
					if ((((this.mapHash[this.tileID] == null)) && ((this.loadingHash[this.tileID] == null))))
					{
						_local12 = (_local10 - _local8);
						if (_local12 < 0)
						{
							_local12 = -(_local12);
						};
						_local13 = (_local11 - _local7);
						if (_local13 < 0)
						{
							_local13 = -(_local13);
						};
						this.sortIndex = (_local12 + _local13);
						this.imgURL = (((this.mapURL + "/") + this.tileID) + ".jpg");
						_local9.push({
							"index":this.sortIndex,
							"url":this.imgURL,
							"pos":{
								"x":_local11,
								"y":_local10
							}
						});
						this.loadingHash[this.tileID] = true;
					};
					_local11++;
				};
				_local10++;
			};
			if (_local9.length > 0)
			{
				_local9.sortOn("index");
				this.waitLoadList = this.waitLoadList.concat(_local9);
				this.delayLoadMapImage();
			};
		}
		
		public function delayLoadMapImage():void
		{
			var item:Object;
			if (this.waitLoadList.length == 0)
			{
				this.loading = false;
				return;
			};
			if (this.loading == true)
			{
				return;
			};
			this.loading = true;
			item = this.waitLoadList.shift();
			LoopManager.setTimeout(function ():void
			{
				GameLoader.getInstance().add(item.url, ResourceItem.PRIORITY_2, false, item.pos, loadTileComplete);
				loading = false;
				delayLoadMapImage();
			}, 100);
		}
		
		protected function loadTileComplete(_arg1:Loader, _arg2:Object):void
		{
			var _local3:Bitmap;
			if (((_arg1) && (_arg2)))
			{
				if ((_arg1.content is Bitmap))
				{
					_local3 = (_arg1.content as Bitmap);
					if (_local3 != null)
					{
						if (this.mapHash == null)
						{
							this.mapHash = new Dictionary();
						};
						delete this.loadingHash[((_arg2.y + "_") + _arg2.x)];
						if (this.mapHash)
						{
							this.mapHash[((_arg2.y + "_") + _arg2.x)] = {
								"x":_arg2.x,
									"y":_arg2.y,
									"bmp":_local3
							}
							_local3.x = (_arg2.x * MAP_TILE_WIDTH);
							_local3.y = (_arg2.y * MAP_TILE_HEIGHT);
							addChild(_local3);
						}
					}
				}
			}
		}
		
		
	}
}